package com.hethongviet.job.helpers;

import java.util.List;

import com.hethongviet.job.R;
import com.hethongviet.job.models.Task;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListItemTaskAdapter extends ArrayAdapter<Task>
{
	private final List<Task> values;
	private final Activity activity;

	/* Make higher performance */
	private static class ViewHolder
	{
		public TextView _textTaskCode;
		public TextView _textTaskName;
		public TextView _textTaskProjectName;
		public TextView _textTaskWorker;
		public TextView _textTaskDateMustStart;
		public TextView _textTaskDateMustFinish;
	}

	public ListItemTaskAdapter(Activity activity, List<Task> values)
	{
		super(activity, R.layout.list_item_task, values);
		this.values = values;
		this.activity = activity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View rowView = convertView;
		final ViewHolder viewHolder;

		if (rowView == null)
		{
			LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = layoutInflater.inflate(R.layout.list_item_task, parent,
					false);
			viewHolder = new ViewHolder();
			viewHolder._textTaskCode = (TextView) rowView.findViewById(R.id.textTaskCode);
			viewHolder._textTaskDateMustFinish = (TextView) rowView.findViewById(R.id.textTaskDateMustFinish);
			viewHolder._textTaskDateMustStart = (TextView) rowView.findViewById(R.id.textTaskDateMustStart);
			viewHolder._textTaskName = (TextView) rowView.findViewById(R.id.textTaskName);
			viewHolder._textTaskProjectName = (TextView) rowView.findViewById(R.id.textTaskProjectName);
			viewHolder._textTaskWorker = (TextView) rowView.findViewById(R.id.textTaskWorker);
			rowView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) rowView.getTag();
		}
		/* Initiate items */
		final Task currentTask = values.get(position);
		int colorCurrentTask = Task.getStatusColor(activity, currentTask.getStatus());
		
		viewHolder._textTaskCode.setText(activity.getString(R.string.textTaskCode)
				+ " " + currentTask.getCode());
		viewHolder._textTaskCode.setTextColor(colorCurrentTask);
		
		viewHolder._textTaskDateMustFinish.setText(activity.getString(R.string.textTaskDateMustFinish)
				+ " " + currentTask.getDateRequestFinish());
		viewHolder._textTaskDateMustFinish.setTextColor(colorCurrentTask);

		viewHolder._textTaskDateMustStart.setText(activity.getString(R.string.textTaskDateMustStart)
				+ " " + currentTask.getDateRequestStart());
		viewHolder._textTaskDateMustStart.setTextColor(colorCurrentTask);

		viewHolder._textTaskName.setText(activity.getString(R.string.textTaskName)
				+ " " + currentTask.getName());
		viewHolder._textTaskName.setTextColor(colorCurrentTask);

		viewHolder._textTaskProjectName.setText(activity.getString(R.string.textTaskProjectName)
				+ " " + currentTask.getProjectCode());
		viewHolder._textTaskProjectName.setTextColor(colorCurrentTask);

		viewHolder._textTaskWorker.setText(activity.getString(R.string.textTaskWorker)
				+ " " + currentTask.getWorker());
		viewHolder._textTaskWorker.setTextColor(colorCurrentTask);

		/* Return view */
		return rowView;
	}
}
